module.exports = {
  // 获取套餐列表
  async getPackageList(category = 'all') {
    const db = uniCloud.database()
    const now = new Date()
    
    try {
      // 构建查询条件
      const where = {
        status: 1 // 上架状态
      }
      
      if (category !== 'all') {
        where.category = category
      }
      
      // 获取套餐数据
      const packageRes = await db.collection('package')
        .where(where)
        .field({
          _id: true,
          name: true,
          description: true,
          price: true,
          duration: true,
          cell_type: true,
          images: true,
          sold: true,
          create_time: true
        })
        .orderBy('price', 'asc')
        .get()
      
      return {
        code: 0,
        message: '获取套餐成功',
        data: packageRes.data
      }
    } catch (e) {
      console.error('获取套餐失败:', e)
      return {
        code: 500,
        message: '获取套餐失败',
        data: null
      }
    }
  },
  
  // 获取套餐详情
  async getPackageDetail(packageId) {
    const db = uniCloud.database()
    
    try {
      // 获取套餐基本信息
      const packageRes = await db.collection('package')
        .doc(packageId)
        .field({
          _id: true,
          name: true,
          description: true,
          price: true,
          duration: true,
          cell_type: true,
          images: true,
          sold: true,
          create_time: true
        })
        .get()
      
      if (!packageRes.data || packageRes.data.length === 0) {
        return {
          code: 404,
          message: '套餐不存在'
        }
      }
      
      const packageData = packageRes.data[0]
      
      // 获取适用站点
      const depotRes = await db.collection('depot')
        .where({
          status: 1
        })
        .field({
          _id: true,
          name: true,
          address: true
        })
        .get()
      
      return {
        code: 0,
        message: '获取套餐详情成功',
        data: {
          ...packageData,
          depots: depotRes.data
        }
      }
    } catch (e) {
      console.error('获取套餐详情失败:', e)
      return {
        code: 500,
        message: '获取套餐详情失败',
        data: null
      }
    }
  }
}